<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产地" prop="factoryId">
        <el-select v-model="queryParams.factoryId" placeholder="请选择" filterable clearable @change="factoryChange">
          <el-option v-for="team in factoryList" :key="team.deptId" :label="team.deptName" :value="team.deptId" />
        </el-select>
      </el-form-item>
      <el-form-item label="班组" prop="teamId">
        <el-select v-model="queryParams.teamId" placeholder="请选择" clearable>
          <el-option v-for="team in teamList" :key="team.deptId" :label="team.deptName" :value="team.deptId" />
        </el-select>
      </el-form-item>
      <el-form-item label="月份" prop="month">
        <el-select v-model="queryParams.month" placeholder="请选择" clearable>
          <el-option
            v-for="dict in  dict.type.month_year"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="周数" prop="monthWeek">
        <el-select v-model="queryParams.monthWeek" placeholder="请选择" clearable>
          <el-option
            v-for="dict in  dict.type.month_week"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['order:plandetail:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="plandetailList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="合同号" align="center" prop="contractCode" />
      <el-table-column label="订单编码" align="center" prop="orderCode" />
      <el-table-column label="制造公司名称" align="center" prop="factoryName" />
      <el-table-column label="制造班组名称" align="center" prop="teamName" />
      <el-table-column label="物料编码" align="center" prop="bomCode" />
      <el-table-column label="物料名称" align="center" prop="bomName" />
      <el-table-column label="排产日期" align="center" prop="planDate" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="排产产能" align="center" prop="planCapacity" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAdjust(scope.row)"
            v-hasPermi="['order:plandetail:list']"
          >申请调整排期</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

     <!-- 排产调整对话框 -->
     <el-dialog
      title="排产调整"
      :visible.sync="adjustopen"
      width="800px"
      append-to-body
    >
      <el-form ref="form2" :model="form2" :rules="rules" label-width="120px">
        <el-row :gutter="5">
          <el-col :span="12">
            <el-form-item label="旧的完成时间" prop="planEndDate1">
              <el-date-picker
                v-model="form2.planEndDate1"
                type="date"
                value-format="yyyy-MM-dd"
                disabled
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="新预计完成时间" prop="planEndDate">
              <el-date-picker
                clearable
                v-model="form2.planEndDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择排产时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel2">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPlandetail, getPlandetail, delPlandetail, addPlandetail, updatePlandetail } from "@/api/sm/order/plandetail";
import { listTeam,listFactory } from "@/api/system/dept";
export default {
  name: "Plandetail",
  dicts: ['month_year','month_week'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单排产明细表格数据
      plandetailList: [],
      factoryList: [],
      teamList: [],
      adjustWplanSelected: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      adjustopen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contractCode: null,
        monthWeek: null,
        factoryId: null,
        teamId: null,
        month: null,
      },
      // 表单参数
      form: {},
      form2: { // 表单参数 - 调整计算的参数
        mId: null,
        bomCode: null,
        planStartDate: null,
        planEndDate: null,
        planStartDate1: null, // 原来的开始日期
        planEndDate1: null, // 原来的结束日期
      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.queryParams.month = this.getCurrentDate();
    this.getList();
    this.getFactoryList();
  },
  methods: {
    cancel2() {
      this.adjustopen = false;
    },
    reset2(){
      this.form2 = {
        mId: null,
        planStartDate: null,
        planEndDate: null
      };
      this.resetForm("form2");
    },
    handleAdjust(row) {
      this.reset2();
      this.adjustWplanSelected = row;
      this.adjustopen = true;
      this.form2.id = row.id;
      this.form2.workorderId = row.workorderId;
      this.form2.planStartDate = row.planDate;
      this.form2.planEndDate = row.planDate;
      this.form2.factoryId = row.factoryId;
      this.form2.teamId = row.teamId;
      this.form2.bomCode = row.bomCode;

      // 原来的结束日期
      this.form2.planStartDate1 = row.planStartDate;
      if (!this.form2.planEndDate1) {
        this.form2.planEndDate1 = row.planEndDate;
      }
      console.log("form2表单-调整赋值", this.form2);
    },
    getCurrentDate() {
      const now = new Date();
      const year = now.getFullYear();
      const month = now.getMonth() + 1; // 月份是从0开始的，所以需要加1
      return `${year}${month.toString().padStart(2, '0')}`;
    },
    getFactoryList() {
      this.loading = true;
      listFactory({"level":2}).then(response => {
        this.factoryList = response.data;
        this.queryParams.factoryId = this.factoryList[0].deptId;
        this.getTeamList(this.queryParams.factoryId);
        this.loading = false;
      });
    },
    factoryChange(){
      this.getTeamList(this.queryParams.factoryId);
    },
    /** 查询订单信息列表 */
    getTeamList(parentId) {
      this.loading = true;
      listTeam({"parentId":parentId}).then(response => {
        this.teamList = response.data;
        this.loading = false;
      });
    },
    /** 查询订单排产明细列表 */
    getList() {
      this.loading = true;
      listPlandetail(this.queryParams).then(response => {
        this.plandetailList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderCode: null,
        productId: null,
        factoryName: null,
        factoryId: null,
        teamName: null,
        teamId: null,
        bomCode: null,
        bomName: null,
        planDate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单排产明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPlandetail(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单排产明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePlandetail(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPlandetail(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单排产明细编号为"' + ids + '"的数据项？').then(function() {
        return delPlandetail(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('order/plandetail/export', {
        ...this.queryParams
      }, `plandetail_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
